<template>
  <div class="app-container">
    <div class="login-con" v-if="!showNav">
      <router-view></router-view>
    </div>
    <div class="func-con" v-else>
      <div class="navbar">
        <div class="back" @click="goBack" v-show="flag"></div>
        <div class="nav-title">{{ title }}</div>
      </div>
      <!-- 路由的容器坑 -->
      <div class="content">
        <transition>
          <keep-alive>
            <router-view v-if="$route.meta.keepAlive"></router-view>>
          </keep-alive>
        </transition>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      showNav: false, //默认不显示顶部导航栏
      flag: true, //默认显示
      title: "",
    };
  },
  methods: {
    goBack() {
      //返回上一页
      this.$router.go(-1);
    },
  },
  watch: {
    //当页面刷新的时候，不会触发watch中监听的路由地址的变化
    "$route.path": function (newVal, oldVal) {
      if (
        newVal === "/login" ||
        newVal === "/register" ||
        newVal === "/changeWord" ||
        newVal === "/idssp/order/confirmOrder" ||
        newVal === "/idssp/order/myAdress" ||
        newVal === "/idssp/order/editorAdress" ||
        newVal === "/idssp/order/addAdress" ||
        newVal === "/idssp/order/paySuccess" ||
        newVal === "/idssp/order/myOrder"
      ) {
        this.showNav = false;
      } else {
        this.showNav = true;
        if (newVal === "/idssp/home") {
          this.flag = false;
        } else {
          this.flag = true;
        }
        this.title = document.title;
      }
    },
  },
  // 所以得在created中先判断一次才行
  created() {
    this.title = document.title;
    if (this.$route.path === "/login") {
      this.showNav = false;
    } else {
      this.showNav = true;
      if (this.$route.path === "/idssp/home") {
        this.flag = false;
      } else {
        this.flag = true;
      }
      this.title = document.title;
    }
  },
  computed: {
    ...mapGetters(["totalCount"]),
  },
};
</script>

// 这里写的样式，得符合SCSS语法，并且 样式是 当前组件私有的，不会成为全局的样式，也不会应用给子组件
<style lang="scss" scoped>
.app-container {
  // padding-top: 40px;
  // 当组件切换时，一瞬间页面的宽度会变成正常时的两倍，所以此时需要隐藏超出屏幕宽度的区域
  // height: 100vh;
  overflow: hidden;
  padding-bottom: 50px; //如果不写则页面会显示不完全
  .navbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 99;
    display: flex;
    width: 100%;
    height: 44px;
    background-color: #e25b45;
    text-align: center;
    color: white;
    font-size: 16px;
    align-items: center;
    .back {
      width: 50px;
      height: 100%;
      position: absolute;
      left: 20px;
      background: url("../assets/img/back.png") no-repeat;
      background-size: 40% 40%;
      background-position-y: center;
    }
    .nav-title {
      margin: 0 auto;
    }
  }
  .content {
    padding-top: 44px;
  }
}
//动画效果
.v-enter {
  opacity: 0;
  // 让即将进入的页面向右偏移%100 屏幕宽度
  transform: translateX(100%);
}
.v-leave-to {
  opacity: 0;
  // 让即将进入的页面向右偏移%100 屏幕宽度
  transform: translateX(-100%);
  position: absolute; //让即将离开的元素不占用页面位置，以至于下一个页面的元素能够在同一位置出现
}
.v-enter-active,
.v-leave-active {
  transition: all 0.4s ease;
}
</style>